﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace AGameOfThrones.timer
{
    /// <summary>
    /// Interaction logic for timer.xaml
    /// </summary>
    public partial class timer : UserControl
    {
        protected delegate void RefreshDelegate();
        private System.Threading.Thread updateThread = null;

        public timer()
        {
            InitializeComponent();
        }
    
        public DateTime StopTime { get; set; }
        public TimeSpan TimeRemaining { get; set; }

        public bool AutoUpdate
        {
            get
            {
                return updateThread != null;
            }
            set
            {
                if (updateThread != null)
                {
                    try
                    {
                        updateThread.Abort();
                    }
                    catch (Exception)
                    { }
                }

                if (value)
                {
                    updateThread = new Thread(delegate()
                    {
                        try
                        {
                            while (true)
                            {
                                bool complete = false;
                                TimeSpan remaining = StopTime.Subtract(DateTime.Now);

                                if(remaining.TotalMilliseconds > 0)
                                {
                                    this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new RefreshDelegate(delegate()
                                    {
               
                                       this.TimeRemaining = remaining;
                                       complete = true;
                                    }));
                                }
                                else
                                {
                                    break;
                                }

                                #region pause
                                try
                                {
                                    do
                                    {
                                        Thread.Sleep(900);
                                    }
                                    while (!complete);
                                }
                                catch (Exception)
                                {
                                }
                                #endregion
                            }
                        }
                        catch (Exception)
                        {
                        }
                    });
                    updateThread.Name = "Clock Thread";
                    updateThread.Start();
                }
            }
        }
    }
}
